/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.sober.Criteria;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.dao.TemplateDAO;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.BeanUtil;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: chenYuan
 * Date: 12-11-30
 * Time: 下午3:43
 * 通用DAO模板
 * com.jspx.txweb.dao.impl.TemplateDAOImpl
 */
public class TemplateDAOImpl extends JdbcOperations implements TemplateDAO {
    private static final Logger log = LoggerFactory.getLogger(TemplateDAOImpl.class);
    private Class tableClass;
    public TemplateDAOImpl()
    {

    }

    public String getClassName()
    {
        if (tableClass==null) return StringUtil.empty;
        return tableClass.getName();
    }

    @Safety(request = false)
    public void setClassName(String className) throws Exception {
        tableClass = ClassUtil.loadClass(className);
    }

    public Class getClassType() throws Exception {
        return tableClass;
    }


    private String namespace = StringUtil.empty;
    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    public String getNamespace()
    {
        return namespace;
    }

    /**
     *
     * @param id    id
     * @return 载入
     * @throws Exception
     */
    public Object load(Long id) throws Exception
    {
        return super.load(tableClass,id);
    }

    /**
     *
     * @param id  id
     * @return  对象
     * @throws Exception
     */
    public Object get(Long id) throws Exception
    {
        return super.load(tableClass,id);
    }
    /**
     * @param ids  id
     * @return     删除
     * */
    public boolean delete(Long[] ids) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object obj =  get(tableClass, id);
                if (obj != null) delete(obj);
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids, StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }


    /**
     * @param ids id
     * @param sortType 排序
     * @return  更新排序
     */
    public boolean updateSortType(Long[] ids, int sortType) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object obj =  get(tableClass, id);
                if (obj != null)
                {
                    BeanUtil.setSimpleProperty(obj, "sortType", sortType);
                    super.update(obj, new String[]{"sortType"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids, StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }

    /**
     *
     *
     * @param ids  id
     * @return 更新排序日期
     */
    public boolean updateSortDate(Long[] ids) throws Exception
    {
        if (ArrayUtil.isEmpty(ids)) return false;
        try
        {
            for (Long id : ids)
            {
                Object uploadFile = get(tableClass, id);
                if (uploadFile != null)
                {
                    BeanUtil.setSimpleProperty(uploadFile, "sortDate", new Date());
                    super.update(uploadFile, new String[]{"sortDate"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids, StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }



    /**
     *
     * @param field 字段
     * @param find 查询条件
     * @param term 条件
     * @param sortString 排序
     * @param uid      用户id
     * @param page     页数
     * @param count   返回数量
     * @return  返回列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List getList(String[] field,String[] find,String term,String sortString,long uid,int page, int count) throws Exception
    {
        if (StringUtil.isNULL(sortString)) sortString="createDate:D";
        Criteria criteria = createCriteria(tableClass);
        if (ClassUtil.haveMethodsName(tableClass,"setNamespace"))
            criteria = criteria.add(Expression.eq("namespace", namespace));

        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field, find));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return SSqlExpression.getSortOrder(criteria, sortString).setCurrentPage(page).setTotalCount(count).list(false);
    }


    /**
     *
     * @param field 字段
     * @param find 查询条件
     * @param term 条件
     * @param uid  用户id
     * @return    得到记录条数
     * @throws Exception
     */
    public int getCount(String[] field,String[] find,String term,long uid) throws Exception
    {
        Criteria criteria = createCriteria(tableClass);
        if (ClassUtil.haveMethodsName(tableClass,"setNamespace"))
            criteria = criteria.add(Expression.eq("namespace", namespace));
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(field)) {
            criteria = criteria.add(Expression.find(field,find));
        }
        if (uid>0)
        {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }

}